package com.ltu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ltu.domain.mp_entity.Role;
import com.ltu.mapper.RoleMapper;
import com.ltu.model.request.account.RoleCommonReq;
import com.ltu.model.request.base.PageReqData;
import com.ltu.model.response.base.CodeDataResp;
import com.ltu.model.response.CodeResp;
import com.ltu.service.RoleService;
import com.ltu.util.BeanMapper;
import com.ltu.util.common.MpPageUtil;
import com.ltu.util.datetime.DateUtils;
import com.ltu.util.common.StrUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Vescky.L
 * @since 2019-10-23
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Override
    public void save(RoleCommonReq roleCommonReq) {
        if(StrUtils.isTrimNull(roleCommonReq.getId())){
            Role role = new Role();
            BeanMapper.copy(roleCommonReq, role);
            role.setCreateTime(DateUtils.currentSecs().intValue());
			role.setEnable(1);
            role.setPlatformType(1);
            role.setAccountId("0");
            super.save(role);
        }else{
            Role findOne = super.getById(roleCommonReq.getId());
            BeanMapper.copy(roleCommonReq, findOne);
            super.updateById(findOne);
        }
    }

    @Override
    public CodeResp remove(RoleCommonReq roleCommonReq) {
        Role role = super.getById(roleCommonReq.getId());
        //空记录
        if (null == role) return CodeDataResp.valueOfSuccessEmptyData();
        //不可删除
        if (role.getDeletable() == 2) return CodeDataResp.valueOfErrorUnAuthorized();

        super .removeById(role.getId());
        return CodeDataResp.valueOfSuccess();
    }

    @Override
    public CodeDataResp getRoleList(PageReqData req) {
        Page<Role> page = MpPageUtil.getCommonPage(req);
        return CodeDataResp.valueOfSuccess( this.page(page));
    }

    @Override
    public Role getRoleById(String id) {
        Role findOne = super.getById(id);
        if (findOne==null) {
            return null;
        }
        else {
            return findOne;
        }
    }
}
